package com.itheima.restkeeper.generate;

import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Component
public class CustomKeyGenerator implements KeyGenerator {

    /**
     *
     * @param target 执行类实例对象
     * @param method 执行的方法（反射）
     * @param params 执行的方法的参数
     * @return
     */
    @Override
    public Object generate(Object target, Method method, Object... params) {
//        method.getName();
//        target.getClass().getName()

        //[DISH_FLAVOR_23, DISH_FLAVOR_32]
        // 23:32
        Object result = Stream.of(params).map(param ->
            param.toString()
                    .replace("DISH_FLAVOR_","")
                    .replace("[","")
                    .replace("]","")
                    .replace(",",":")
                    .replace(" ","")

        ).collect(Collectors.joining(":"));

        return result;
    }
}
